﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;
using Sweepids.Models.Object;

namespace Sweepids.Models.Dao
{
    public class BidOnDao : Dao
    {
        /// <summary>
        /// Add a new BidOn row to the database
        /// </summary>
        /// <param name="b">The BidOn object to add</param>
        public void Create(BidOn b)
        {
            try
            {
                var myCommand = new OracleCommand("INSERT INTO SWEEP_BID_ON " +
                                                  "(ITEM_ID, USERNAME, NUM_BIDS_FOR_USER) " +
                                                  "VALUES " +
                                                  "(:ITEM_ID, :USERNAME, :NUM_BIDS_FOR_USER)", myConnection);
                RunCommand(myCommand, b.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("BidOnDao::Create(BidOn): " + e.Message);
                throw;
            }
        }

        /// <summary>
        /// Get a BidOn row
        /// </summary>
        /// <param name="itemId">ItemId part of the primary key</param>
        /// <param name="username">Username part of the primary key</param>
        /// <returns>A BidOn object</returns>
        public BidOn Read(int itemId, string username)
        {
            try
            {
                var result = new BidOn {ItemId = itemId, Username = username};
                var myCommand = new OracleCommand("SELECT * FROM SWEEP_BID_ON " +
                                                  "WHERE ITEM_ID=:ITEM_ID " +
                                                  "AND USERNAME=:USERNAME", myConnection);
                var myReader = RunCommand(myCommand, result.OracleParameters, true);
                while (myReader.Read())
                {
                    result = new BidOn(myReader);
                }
                myReader.Close();
                myConnection.Close();
                return result;
            }
            catch (Exception e)
            {
                Debug.WriteLine("BidOnDao::Read(int, string): " + e.Message);
                throw;
            }
        }

        /// <summary>
        /// Get a list of bidons based on the item id
        /// </summary>
        /// <param name="itemId">the item id</param>
        /// <returns>a list of bidon objects</returns>
        public List<BidOn> Read(decimal itemId)
        {
            try
            {
                var result = new List<BidOn>();
                var parameters = new BidOn { ItemId = itemId };
                var myCommand = new OracleCommand("SELECT * FROM SWEEP_BID_ON " +
                                                  "WHERE ITEM_ID=:ITEM_ID", myConnection);
                var myReader = RunCommand(myCommand, parameters.OracleParameters, true);
                while (myReader.Read())
                {
                    result.Add(new BidOn(myReader));
                }
                myReader.Close();
                myConnection.Close();
                return result;
            }
            catch (Exception e)
            {
                Debug.WriteLine("BidOnDao::Read(int): " + e.Message);
                throw;
            }
        }

        /// <summary>
        /// Get a list of bidon objects based on the username
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public List<BidOn> Read(string username)
        {
            try
            {
                var result = new List<BidOn>();
                var parameters = new BidOn { Username = username };
                var myCommand = new OracleCommand("SELECT * FROM SWEEP_BID_ON " +
                                                  "WHERE USERNAME = :USERNAME", myConnection);
                var myReader = RunCommand(myCommand, parameters.OracleParameters, true);
                while (myReader.Read())
                {
                    result.Add(new BidOn(myReader));
                }
                myReader.Close();
                myConnection.Close();
                return result;
            }
            catch (Exception e)
            {
                Debug.WriteLine("BidOnDao::Read(string): " + e.Message);
                throw;
            }
        }

        /// <summary>
        /// Update a BidOn row in the database
        /// </summary>
        /// <param name="b">The BidOn object to change with the new values</param>
        public void Update(BidOn b)
        {
            try
            {
                var myCommand = new OracleCommand("UPDATE SWEEP_BID_ON SET " +
                                                  "ITEM_ID=:ITEM_ID, " +
                                                  "USERNAME=:USERNAME, " +
                                                  "NUM_BIDS_FOR_USER=:NUM_BIDS_FOR_USER " +
                                                  "WHERE ITEM_ID=:ITEM_ID " +
                                                  "AND USERNAME=:USERNAME", myConnection);
                RunCommand(myCommand, b.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("BidOnDao::Update(BidOn): " + e.Message);
                throw;
            }
        }

        /// <summary>
        /// Remove a BidOn row in the database
        /// </summary>
        /// <param name="b">The BidOn object representing the row to delete in the database</param>
        public void Delete(BidOn b)
        {
            try
            {
                var myCommand = new OracleCommand("DELETE FROM SWEEP_BID_ON " +
                                                 "WHERE ITEM_ID=:ITEM_ID " +
                                                 "AND USERNAME=:USERNAME", myConnection);
                RunCommand(myCommand, b.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("BidOnDao::Delete(BidOn): " + e.Message);
                throw;
            }
        }
    }
}